package org.github.manx98.jlibhac.common;

import lombok.extern.slf4j.Slf4j;

/**
 * @author Manx98
 * @version 1.0
 * @since 2022/10/3 15:38
 */
@Slf4j
public class Utilities {
    public static boolean spansEqual(byte[] a, int offset1, byte[] b, int offset2, int len) {
        if (a.length - offset1 != len && a.length - offset1 != b.length - offset2) {
            return false;
        }
        for (int i = 0; i < len; i++) {
            if (a[i + offset1] != b[i + offset2]) {
                log.info("spansEqual a[{}+{}]={}, b[{}+{}]={}", i, offset1, a[i + offset1], i, offset2, b[i + offset2]);
                return false;
            }
        }
        return true;
    }

    public static void xorArrays(byte[] transformData, int offset, int len, byte[] xorData) {
        for (int i = 0; i < len; i++) {
            transformData[offset + i] ^= xorData[i];
        }
    }

    public static boolean isZeros(byte[] span) {
        for (byte b : span) {
            if (b != 0) {
                return false;
            }
        }

        return true;
    }

    public static int getMasterKeyRevision(byte keyGeneration) {
        if (keyGeneration == 0) {
            return 0;
        }

        return keyGeneration - 1;
    }
}
